/*
 * DIM-SUM操作系统 - extable arm64 lib
 *
 * Copyright (C) 2023 国科础石(重庆)软件有限公司
 *
 * 作者: Lei Ding <dinglei@kernelsoft.com>
 *
 * License terms: GNU General Public License (GPL) version 3
 *
 */

#include <asm/uaccess.h>


static inline unsigned long ex_to_insn(const struct exception_table_entry *x)
{
	return (unsigned long)&x->insn + x->insn;
}


const struct exception_table_entry *
search_extable(const struct exception_table_entry *first,
	       const struct exception_table_entry *last,
	       unsigned long value)
{
	while (first <= last) {
		const struct exception_table_entry *mid;

		mid = ((last - first) >> 1) + first;
		/*
		 * careful, the distance between value and insn
		 * can be larger than MAX_LONG:
		 */
		if (ex_to_insn(mid) < value)
			first = mid + 1;
		else if (ex_to_insn(mid) > value)
			last = mid - 1;
		else {
			return mid;
		}
	}
	return NULL;
}
